package com.mindata.blockmanager.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.base.Preconditions;
import com.mindata.blockmanager.bean.BaseData;
import com.mindata.blockmanager.bean.GridResultDto;
import com.mindata.blockmanager.bean.MemberParam;
import com.mindata.blockmanager.bean.MemberVo;
import com.mindata.blockmanager.bean.QueryPage;
import com.mindata.blockmanager.bean.ResultGenerator;
import com.mindata.blockmanager.manager.MemberGroupManager;
import com.mindata.blockmanager.manager.MemberManager;
import com.mindata.blockmanager.model.Member;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("/admin/member")
@Slf4j
public class MemberAdminController extends BaseController {

    @Resource
    private MemberManager memberManager;
    @Resource
    private MemberGroupManager memberGroupManager;

    /**
     * 联盟查询.
     *
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public GridResultDto<MemberVo> memberList(QueryPage page) {
        GridResultDto memberVos = memberManager.pagingMemberList(page);
        return memberVos;
    }

    @RequestMapping("/listPage")
    public ModelAndView listPage() {
        ModelAndView modelAndView = new ModelAndView("member/member_list");
        modelAndView.addObject("groupMap", memberGroupManager.getGroupMap());
        return modelAndView;
    }


    @RequestMapping("/add")
    @ResponseBody
    public BaseData addMember(MemberParam param) {
        Member member = new Member();
        BeanUtil.copyProperties(param, member);
        member.setId(null);
        member.setCreateTime(new Date());
        member.setUpdateTime(new Date());
        memberManager.addMember(member);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/del")
    @ResponseBody
    public BaseData delMember(long id) {
        memberManager.delMember(id);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/updatePage")
    public ModelAndView updatePage(long id) {
        Preconditions.checkState(id > 0, "error id");
        MemberVo memberVo = memberManager.findById(id);
        Preconditions.checkNotNull(memberVo, "error id");

        ModelAndView modelAndView = new ModelAndView("member/member_update");
        modelAndView.addObject("member", memberVo);
        modelAndView.addObject("groupMap", memberGroupManager.getGroupMap());
        return modelAndView;
    }

    @RequestMapping("/update")
    @ResponseBody
    public BaseData update(MemberParam param) {
        Member member = new Member();
        BeanUtil.copyProperties(param, member);
        member.setCreateTime(new Date());
        member.setUpdateTime(new Date());
        memberManager.updateMember(member);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/addPage")
    public ModelAndView addPage() {
        ModelAndView modelAndView = new ModelAndView("member/member_add");
        modelAndView.addObject("groupMap", memberGroupManager.getGroupMap());
        return modelAndView;
    }


}